package com.cqu.lpss.service.impl;


import com.cqu.lpss.mapper.ProjectMapper;
import com.cqu.lpss.mapper.ScoreDetail2Mapper;
import com.cqu.lpss.mapper.ScoringCriterionsMapper;
import com.cqu.lpss.mapper.SecondScoringMapper;
import com.cqu.lpss.pojo.*;
import com.cqu.lpss.service.SecondScoringInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

@Service
@Slf4j
public class SecondScoringImpl implements SecondScoringInterface {
    @Autowired
    SecondScoringMapper secondScoringMapper;
    @Autowired
    ScoringCriterionsMapper scoringCriterionsMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    ScoreDetail2Mapper scoreDetail2Mapper;
/**
* @Description: 返回secondescoring表格中的数量，为0表示空
* @Param:[]
* @Return: int
* @Date: 2024/5/9
* @Author:liuyawen
*/

    @Override
    public int EmptyNum() {
        return secondScoringMapper.recordnum();
    }

    @Override
    public List<ChartsData> proUnscoreNum() {
        List<ChartsData> professorUnscoreNums =secondScoringMapper.proUnscoreNum();
        return professorUnscoreNums;
    }

    @Override
    public List<Projects> prjSocreByProIndex(Integer proIndex) {
        List<Projects> projectsList = secondScoringMapper.prjScoreByProIndex(proIndex);
        return projectsList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modifyScore(List<Scoredetail2> scoredetail2s) {
        int classId = scoredetail2s.get(0).getSdClass2();
        List<Scoringcriterions> scoringcriterions = scoringCriterionsMapper.findByClass(classId);
        //criterionsId:scWeight
        Map<Integer, Double> criterionIdScWeight = new HashMap<>();
        for(int i=0;i<scoringcriterions.size();i++){
            criterionIdScWeight.put(scoringcriterions.get(i).getScId(),scoringcriterions.get(i).getScWeight());
        }
        double totalscore = 0.0;
        for(int i=0;i<scoredetail2s.size();i++){
            scoredetail2s.get(i).setSdTime2(new Timestamp(System.currentTimeMillis()));
            totalscore+=scoredetail2s.get(i).getSdScore2()*criterionIdScWeight.get(scoredetail2s.get(i).getSdCretierionid2());
        }
        int modifyScoreNum = secondScoringMapper.modifyScore(scoredetail2s.get(0).getSdProid2(), scoredetail2s.get(0).getSdPrjid2(), totalscore, scoredetail2s.get(0).getSdTime2());
        log.info("param:{}",scoredetail2s);
        int recordDetailNum = scoreDetail2Mapper.batch_insertDetail(scoredetail2s);
        int numOfGradeIsNUll = secondScoringMapper.numOfScoreisNull();
        if(numOfGradeIsNUll==0){
            List<Projects> projects = projectMapper.list_final();
            for(int i=0;i<projects.size();i++){
                Projects tempprj = projects.get(i);
                Integer prjIndex = tempprj.getPrjIndex();
                List<Secondscoring> secondscoreprjbyprjIndex = secondScoringMapper.findByprjIndex(prjIndex);
                List<Double> grades = new ArrayList<>();
                for(int j = 0;j<secondscoreprjbyprjIndex.size();j++){
                    grades.add(secondscoreprjbyprjIndex.get(j).getSco2Grade());
                }
                Double max = Collections.max(grades);
                Double min = Collections.min(grades);

                for(int j=0;j<grades.size();j++){
                    if(grades.get(j).equals(max)){
                        grades.remove(j);
                        break;
                    }
                }
                for(int j=0;j<grades.size();j++){
                    if(grades.get(j).equals(min)){
                        grades.remove(j);
                        break;
                    }
                }


                // Calculate the average of the remaining numbers
                Double sum = 0.0;
                for (Double grade : grades) {
                    sum += grade;
                }
                log.info("sum:{}",sum);
                Double averageGrade = sum /Double.valueOf(grades.size());
                log.info("averageGrade:{}",averageGrade);
                int ans = projectMapper.modifyFinalGrade(prjIndex,averageGrade);
            }
            projects = projectMapper.list_final();
            // 使用Collections.sort和自定义比较器按prj_score排序
            Collections.sort(projects, Comparator.comparing(Projects::getPrjFinalscore).reversed());

            // 3:通过终审，4：未通过终审
            for (int i = 0; i < projects.size(); i++) {
                Integer status = i < 10 ? 3 : 4;
                projects.get(i).setPrjStatus(status);
                int modifystatus = projectMapper.modifyPrjStatus(projects.get(i).getPrjIndex(),status);
            }

        }
        return recordDetailNum*modifyScoreNum;
    }

    @Override
    public int count() {
        return secondScoringMapper.count();
    }
}
